package org.bozo.im.service.protocols.xmpp.stanza.task.jabber.client;

import org.bozo.im.service.Constants;
import org.bozo.im.service.protocols.xmpp.Capabilities;
import org.bozo.im.service.protocols.xmpp.XMPPSession;
import org.bozo.im.service.protocols.xmpp.stanza.Stanza;
import org.bozo.im.service.protocols.xmpp.stanza.generator.StanzaGenerator;
import org.bozo.im.service.protocols.xmpp.stanza.task.Task;
import org.bozo.im.service.protocols.xmpp.stanza.util.jabber.client.XMPPPresence;
import org.bozo.im.service.protocols.xmpp.util.encoding.Base64;
import org.bozo.im.service.protocols.xmpp.util.encoding.Sha1;
import org.bozo.im.service.protocols.xmpp.util.namespace.Namespace;

import android.util.Log;

public class PresenceTask extends Task {

	public PresenceTask(String pId) {
		super(pId);
	}

	@Override
	public void doTask(XMPPSession pSession, StringBuilder pBuilder) throws Exception {

		// ---------------------------------------------------------------------------------------
		Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.stanza.task.jabber.client.PresenceTask.doTask ---------- Sending presence statuss");
		// ---------------------------------------------------------------------------------------
		
		StringBuilder lHash = new StringBuilder();
		
		lHash.append(Capabilities.getIdentity()).append('<');
		
		for (String lCapability : Capabilities.getCapabilities())
			lHash.append(lCapability).append('<');
		
		StanzaGenerator lCapabilitiesStanza = new StanzaGenerator(Stanza.CAPABILITIES_C);
		lCapabilitiesStanza.setDefaultNamespace(Namespace.CAPABILITIES);
		lCapabilitiesStanza.setAttribute(Namespace.CAPABILITIES, "hash", "sha-1");
		lCapabilitiesStanza.setAttribute(Namespace.CAPABILITIES, "node", "http://code.google.com/p/android-unified-im-client");
		lCapabilitiesStanza.setAttribute(Namespace.CAPABILITIES, "ver", Base64.encode(Sha1.encode(lHash.toString())));
		
		StanzaGenerator lShowStanza = new StanzaGenerator(Stanza.JABBER_CLIENT_SHOW);
		lShowStanza.setDefaultNamespace(Namespace.JABBER_CLIENT);
		lShowStanza.setContent(XMPPPresence.CHAT.toString());
		
		StanzaGenerator lPresenceStanza = new StanzaGenerator(Stanza.JABBER_CLIENT_PRESENCE);
		lPresenceStanza.setDefaultNamespace(Namespace.JABBER_CLIENT);
		lPresenceStanza.addChild(lCapabilitiesStanza);
		lPresenceStanza.addChild(lShowStanza);
		
		lPresenceStanza.toXml(pBuilder);
		
	}

}
